package com.thti.mecool.client.command.result.plan;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.threeti.mecool.core.domain.model.plan.pojo.Plan;
import com.threeti.mecool.core.domain.model.plan.pojo.PlanJob;
import com.threeti.mecool.core.domain.model.plan.pojo.PlanProject;
import com.thti.mecool.client.activity.MyPlanActivity;
import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.base.Cache;
import com.thti.mecool.client.base.HandlerConstant;
import com.thti.mecool.client.command.ICommand;
import com.thti.mecool.client.model.biz.PlanBizImpl;
import com.thti.mecool.client.protocol.plan.PlanProtocolResult;
import com.thti.mecool.client.service.Protocol;
import com.thti.mecool.client.util.TimeUtil;

public class MyPlanCommand implements ICommand {
	PlanBizImpl planBizImpl;
	List<Map<String,Object[]>> list=new ArrayList<Map<String,Object[]>> (); 
	 

	@Override
	public void doCommand(Object o) {
		PlanProtocolResult planProtocolResult = (PlanProtocolResult) o;
		if (planProtocolResult != null) {
			if (planProtocolResult.getStatus() == 1 &&planProtocolResult.getPlan()!=null) {	
				planBizImpl = new PlanBizImpl(Cache.getmContext());
				Plan planServer = planProtocolResult.getPlan();
				if(null!=planServer){
				 if (planBizImpl
				 .findByPlanId(String.valueOf(planServer.getId()),BaseActivity.getCurrentUser().getId())) {
						planBizImpl.delete(String.valueOf(planServer.getId()),false);
						list.add(savePlan(planServer));
				 }

				List<PlanProject> planProjects = planServer.getPlanProjects();
				if (planProjects.size() > 0) {
//					for (int i = 0; i < planProjects.size(); i++) {
//						list.add(saveProject(planProjects.get(i)));
						String proId=null;
						for (int i = 0; i < planProjects.size(); i++) {
							if(proId==null || !proId.equals(planProjects.get(i).getProjectCode())){
								 list.add(saveProject(planProjects.get(i)));
							}
							proId=planProjects.get(i).getProjectCode();
						List<PlanJob> planJobs = planProjects.get(i)
								.getPlanJobs();
						if (planJobs.size() > 0) {
							for (int j = 0; j < planJobs.size(); j++) {
								list.add(saveTask(planJobs.get(j)));
						
							}
						}
					}
				}
				if(null!=list && list.size()>0){
					planBizImpl.savePlan(list);
				}
			  }
				MyPlanActivity.handler.sendEmptyMessage(0xAA);
			}else{
				MyPlanActivity.handler.sendEmptyMessage(0xBB);
			}
		}
	}

	

	public Map<String,Object[]> savePlan(Plan planServer) {
		Map<String,Object[]> map=new HashMap<String, Object[]>();
		map.put(HandlerConstant.PLAN_SQL,new Object[]{BaseActivity.getCurrentUser().getId(),planServer.getId(),planServer.getName(), 
		         0,2,TimeUtil.dateToYYYYMM(planServer.getPlanDate()),planServer.getStatus(),planServer.getDenyNotes(),planServer.getChangeNotes(),planServer.getType(),new Date()});
      return map;
	}

	public Map<String,Object[]> saveProject(PlanProject planProject) {
		Map<String,Object[]> map=new HashMap<String, Object[]>();
		map.put(HandlerConstant.PROJECT_SQL,new Object[]{BaseActivity.getCurrentUser().getId(),planProject.getProjectCode(),planProject.getProjectName(),
				planProject.getPlan().getId(),"2",new Date()});
		return map;
	}

	public Map<String,Object[]> saveTask(PlanJob planJob) {
		Map<String,Object[]> map=new HashMap<String, Object[]>();
		//userId,projectId,taskId,taskName,startTime,endTime,storeId,storeName,planId,isType,isFinish) values(?,?,?,?,?,?,?,?,?,?,?)";	
		    map.put(HandlerConstant.TASK_SQL,new Object[]{BaseActivity.getCurrentUser().getId(),planJob.getPlanProject().getProjectCode(),
		    		planJob.getId(),planJob.getJob().getName(),TimeUtil.dateToString(planJob.getStartTime(),1),
		    		TimeUtil.dateToString(planJob.getEndTime(),1),planJob.getStoreCode(),planJob.getStoreName(),
		    		planJob.getPlanProject().getPlan().getId(),"local",0});
		return map;
	}

	
	@Override
	public String getCommandID() {
		// TODO Auto-generated method stub
		return Protocol.getMyPlan;
	}

	@Override
	public Class<?> getCommandClass() {
		// TODO Auto-generated method stub
		return PlanProtocolResult.class;
	}

}
